const Node = require('./node')

//循环单链表类
class SinglyCircleLinkedList {
    // 构造器
    constructor (head = new Node(null, this.head)) {
        this.head = head 
    }
    // 数组构造
    static createByArr (arr) {
        let scll = new SinglyCircleLinkedList()
        let p = scll.head
        for(let i = 0; i <= arr.length-1; i++) {
            p.next = new Node(arr[i], scll.head)
            p = p.next
        }
        return scll
    }
    //返回描述字符串
    toString () {
        let str = ""
        let p = this.head
        while(p.next != this.head) {
            str += `${p.toString()}-->`
            p = p.next
        }
        return `${str}${p.toString()}-->tohead`
    }

}

module.exports = SinglyCircleLinkedList




//测试用例
// let arr = [1,2,3,4,5,6]
// let scll = SinglyCircleLinkedList.createByArr(arr)
// console.log(scll.toString());